<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2016/3/8
  Time: 13:42
  To change this template use File | Settings | File Templates.
--%>
<%@page import="com.eportal.util.WebUtil"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>应用管理</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <link href="${pageContext.request.contextPath}/static/css/common.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/css/bootstrap.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/font-awesome-4.3.0/css/font-awesome.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/css/select2.min.css"
          rel="stylesheet"/>
	<link href="${pageContext.request.contextPath}/static/plugins/layer/skin/layer.css" rel="stylesheet">

    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/js/select2.min.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugins/layer/layer.js" type="text/javascript"></script>
	<script type="text/javascript"
            src="${pageContext.request.contextPath}/static/plugins/My97DatePicker/WdatePicker.js"></script>
     <script src="${pageContext.request.contextPath}/static/js/jquery.qrcode.min.js" type="text/javascript"></script>
	<style>
		.jqGrid_wrapper{*+margin-left:270px;}
	
		.lt,.col-sm-3,.col-sm-8{float:left}
		.form-group{*+overflow:hidden;}
		.form-group label{width:28%\9;*+width:25%;text-align:right\9;}
		.col-sm-8 .m-t2 {width:40%\9; *+width:36%;*+text-align:left;}
		.form-group .col-sm-8{width:66%\9;*+width:66%;}
		.form-group .form-control{*+width:95%;}
		.form-control{*+height:22px;}
		#sub{*+margin-right:10px;}
		.input-group .form-control{*+z-index:-1;}
		.upload{*+position:relative;}
		.btn-primary{*+position:absolute;*+left:431px;*+top:-35px;*+z-index:3;}
	</style>
</head>
<body>
<div class="col-sm-12">
    <form class="form-horizontal m-t"  id="uploadForm">
        <input type="hidden" name="sid" value="${appStore.sid}">
        <a id="sure" onclick="sure()" hidden="true"></a>
       <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require"></i>APP名称:</label>
            <div class="col-sm-8">
                <input class="form-control" name="appName" size="30"  value="${appStore.appName}" disabled="disabled"/>
            </div>
        </div>
         <div class="form-group">
            <label class="col-sm-3 control-label"><i class="require">*</i>下载页是否使用默认顶部图:</label>
            <div class="col-sm-8">
                <div class="col-sm-2 m-t2">
                    <input type="radio" <c:if test="${empty appStore.download_img1}">checked</c:if> name="isUseDefaultTopImg" size="30" value="1" img-id='1' class='imgRadio'/><label>默认</label>
                </div>
                <div class="col-sm-2 m-t2">
                    <input type="radio" <c:if test="${!empty appStore.download_img1}">checked</c:if>  name="isUseDefaultTopImg" size="30" value="0" img-id='1' class='imgRadio'/><label>自定义</label>
                </div>
            </div>
        </div>
        <div class="form-group" id='d-1' <c:if test="${empty appStore.download_img1}">style="display: none;"</c:if>>
            <label class="col-sm-3 col-sm-3 control-label">下载页顶部图:</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control" name="content1" id='content1' value="${appStore.download_img1}"  readonly size="30"/>
                    <div class="input-group-addon upload">
                        <input class="form-control upload" id="upload1" type="file" name="upload1"
                               style="position: absolute;width: 60px;opacity: 0;"/>
                        <a class="btn btn-primary">上传</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label"><i class="require">*</i>下载页是否使用默认底部图:</label>
            <div class="col-sm-8">
                <div class="col-sm-2 m-t2">
                    <input type="radio" <c:if test="${empty appStore.download_img2}">checked</c:if>   name="isUseDefaultBottomImg" size="30" value="1" img-id='2'  class='imgRadio'/><label>默认</label>
                </div>
                <div class="col-sm-2 m-t2">
                    <input type="radio" <c:if test="${!empty appStore.download_img2}">checked</c:if>  name="isUseDefaultBottomImg" size="30" value="0" img-id='2'  class='imgRadio'/><label>自定义</label>
                </div>
            </div>
        </div>
        <div class="form-group" id='d-2' <c:if test="${empty appStore.download_img2}">style="display: none;"</c:if>>
            <label class="col-sm-3 col-sm-3 control-label">下载页底部图:</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control" name="content2" id='content2' value="${appStore.download_img2}"  readonly size="30"/>
                    <div class="input-group-addon upload">
                        <input class="form-control upload" id="upload2" type="file" name="upload2"
                               style="position: absolute;width: 60px;opacity: 0;"/>
                        <a class="btn btn-primary">上传</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require"></i>下载地址:</label>
            <div class="col-sm-8">
            	<div class="col-sm-8 m-t2">
            	<label>
                	${downloadUrl}
                </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3  control-label"><i class="require"></i>下载二维码:</label>
            <div class="col-sm-8">
            	<div class="col-sm-2 m-t2">
            	<label>
                <a href="javascript:void(0)" onclick="downloadQr()">点击下载</a>
                </label>
                </div>
            </div>
        </div>
    </form>
    <div id='code' style="display: none;"></div>
</div>
<script type="text/javascript">
 
	$("select").val('${appStore.appType}');	
	$(function () {
		$("#upload1").change(function () {
	        var filename = $(this).val();
	        var index = filename.lastIndexOf("\\");
	        $("input[name='content1']").val(filename.substring(index + 1));
	    });
		
		$("#upload2").change(function () {
	        var filename = $(this).val();
	        var index = filename.lastIndexOf("\\");
	        $("input[name='content2']").val(filename.substring(index + 1));
	    });
		
		$(".imgRadio").change(function () {
	        checkImgRadio(this);
	    });
		
		  
    	var str = " ${downloadUrl}";
    	$('#code').qrcode({
			width: 720,
			height:720,
			text: str
		});
	})
	function downloadQr(){
		try{
			window.location='${pageContext.request.contextPath}/admin/appstore_downloadQrcode.action?sid=${appStore.sid}';
			/* var canvas = $("canvas").get(0);
			var img = canvas.toDataURL("image/png");
			downloadFile("qr.png",img); */
		}catch(e){
			console.log(e.message);
		}
		
		 //window.location  = img;
	}
	
    function downloadFile(fileName, content){
        
        var aLink = document.createElement('a');
        var blob = base64Img2Blob(content); //new Blob([content]);
      
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("click", false, false);//initEvent 不加后两个参数在FF下会报错
        aLink.download = fileName;
        aLink.href = URL.createObjectURL(blob);
 
        aLink.dispatchEvent(evt);
    }    
    
    function base64Img2Blob(code){
        var parts = code.split(';base64,');
        var contentType = parts[0].split(':')[1];
        var raw = window.atob(parts[1]);
        var rawLength = raw.length;

        var uInt8Array = new Uint8Array(rawLength);

        for (var i = 0; i < rawLength; ++i) {
          uInt8Array[i] = raw.charCodeAt(i);
        }

        return new Blob([uInt8Array], {type: contentType}); 
    }
    
	function checkImgRadio(obj){
		if($(obj).is(':checked')){
			var val = $(obj).val();
			var imgId = $(obj).attr("img-id");
			if(val=='1'){
				$("#d-"+imgId).hide();				
			}else{
				$("#d-"+imgId).show();
			}
		}else{
			
		}
		
	}
	function sure(){
		var isUseDefaultBottomImg=$('input:radio[name="isUseDefaultTopImg"]:checked').val();
		if(isUseDefaultBottomImg=='0'){
			if($("#content1").val()==''){
				alert("请选择下载页顶部图片");
				return;
			}
		}
		
		var isUseDefaultBottomImg=$('input:radio[name="isUseDefaultBottomImg"]:checked').val();
		if(isUseDefaultBottomImg=='0'){
			if($("#content2").val()==''){
				alert("请选择下载页底部图片");
				return;
			}
		}
		var formData = new FormData($( "#uploadForm" )[0]); 
    	$.ajax({  
         url: '${pageContext.request.contextPath}/admin/appstore_set.action' ,  
         type: 'POST',  
         data: formData,  
         async: false,  
         cache: false,  
         contentType: false,  
         processData: false,  
         success: function (result) {  
              result = JSON.parse(result)
              if (result.code == 1) {
            		 var index = parent.layer.getFrameIndex(window.name);
                  parent.layer.close(index); 
              }else{
              	alert(result.msg);
              }
         },  
         error: function (returndata) {  
             alert(returndata);  
         }  
    	});
    }
</script>
</body>
</html>
